﻿using Microsoft.Xna.Framework;

using block_explorer.NoiseSystem;
using block_explorer.NoiseSystem.Nodes;

namespace block_explorer
{
    class BasicHillsBuilder : IMapBuilder
    {
        INode mNoiseChain;

        public BasicHillsBuilder()
        {
            BuildNoiseChain();
        }

        public Block GetBlock(Vector3 worldPosition)
        {
            double v = mNoiseChain.Value(worldPosition.X, worldPosition.Y, worldPosition.Z);
            return new Block(v > 0.5 ? Block.ContentType.DIRT : Block.ContentType.AIR);
        }

        private void BuildNoiseChain()
        {
            INode baseNoise = new SimplexBasis();

            // testing ridges
            INode terrain;
            {
                INode ridges = new Fractal(baseNoise, 4, 0.5, 0.5, 0.007, 2.0);
                INode heightmap = new ScaleDomain(ridges, 1.0, 0.0, 1.0);
                INode scaledHeightmap = new OffsetScale(heightmap, 0.0, 20.0);
                INode density = new Gradient(0, 80, 0, 0, 0, 0);
                INode hillDensity = new TranslateDomain(density, null, scaledHeightmap, null);
                INode blocks = new Select(hillDensity, 1.0, 0.0, 0.5, 0.0);

                terrain = blocks;
            }

            mNoiseChain = terrain;
        }
    }
}
